package com.rockxin.cat.utils.imageUtils;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rockxin.cat.R;


/**
 * <b>类名称：   </b> ActionSheetDialog <br/>
 * <b>类描述：   </b><br/>
 * <b>创建人：   </b> leishun <br/>
 * <b>创建时间： </b> 2017/4/12 上午9:48 <br/>
 * <b>修改人：   </b><br/>
 * <b>修改时间： </b><br/>
 * <b>修改备注： </b><br/>
 *
 * @version 1.0.0 <br/>
 */
public class ActionSheetDialog extends DialogFragment implements View.OnClickListener {

    private Dialog dialog;
    private FragmentActivity context;
    private ActionSheetListener actionSheetListener;

    public void setActionSheetListener(ActionSheetListener actionSheetListener) {
        this.actionSheetListener = actionSheetListener;
    }

    public void bindActivity(FragmentActivity context) {
        this.context = context;
    }

    @NonNull
    @Override

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        //填充对话框的布局
        View inflate = LayoutInflater.from(context).inflate(R.layout.dialog_from_bottom, null);
        //初始化控件
        TextView choosePhoto = (TextView) inflate.findViewById(R.id.choosePhoto);
        TextView takePhoto = (TextView) inflate.findViewById(R.id.takePhoto);
        TextView cancel = (TextView) inflate.findViewById(R.id.cancel);
        cancel.setOnClickListener(this);
        choosePhoto.setOnClickListener(this);
        takePhoto.setOnClickListener(this);
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //设置Dialog从窗体底部弹出
        //  dialogWindow.setGravity(Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow != null ? dialogWindow.getAttributes() : null;
        lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        lp.width = LinearLayout.LayoutParams.MATCH_PARENT;
//       将属性设置给窗体
        dialogWindow.setAttributes(lp);
        lp.gravity = Gravity.BOTTOM;
        return dialog;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.takePhoto:
                if (actionSheetListener != null) {
                    actionSheetListener.onTakePhoto();
                    dismiss();
                }
                break;
            case R.id.choosePhoto:
                if (actionSheetListener != null) {
                    actionSheetListener.onChooseFrom();
                    dismiss();
                }
                break;
            case R.id.cancel:
                dialog.dismiss();
                break;
            default:
                dismiss();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }

    public interface ActionSheetListener {

        void onTakePhoto();

        void onChooseFrom();
    }
}
